<?php
	$_nav="info";
	$_title='资讯';
	include_once 'head.php';
	$user_praise='user_praise';				//会员点赞表
	$user_fans='user_fans';					//会员粉丝表
	$user_collection='user_collection';		//会员收藏表
	$infoc=new info();
	$userc=new user();

	$info_id=$_GET['id'];					//资讯id


	/***评论***/
	if($_GET['action']=='comment'){
		$table='info_comment';
		$reply_id=$_POST['reply_id']; //回复ID
		$comment=$_POST['comment'];	  //评论内容
		$info_id=$_POST['info_id'];//服务ID
		$user_type=$_POST['user_type'];	//回复类型

		if($user_id!=''){
			$array=array('info_id'=>$info_id,'user_id'=>$user_id,'comment'=>$comment,'reply_id'=>$reply_id,'reply_type'=>$user_type,'create_time'=>time());

			//判断是否自己回复自己(暂时关闭)
			// if($reply_id==$user_id){
			// 	$array['reply_id']=0;
			// }

			$res=$commonc->insert($table,$array);
			if($res){
				$mes='评论成功';
				$time=date('Y-m-d H:i:s',$array['create_time']);


				// $wheres=' AND id="'.$user_id.'"';
				// $key=$type=='user'?'a.*':'a.*,a.name as account';
				// $user=$infoc->adminInfo($type,$wheres,$key);

				$user=$userc->userInfo($user_id);
				$userName=$user['account'];
				$userImg=$user['avatar'];

				/*回复AJAX-start*/
				if($array['reply_id']>0){
					if($user_type=='user'){
						$reply=$userc->userInfo($reply_id);
						$replyName=$reply['account'];
						$reply="回复&nbsp;@<a href=user_main.php?type=user&id=".$reply_id.">".$replyName."</a>：";
					}else{
						$replyName='小编';
						$reply="回复&nbsp;@<a href=user_main.php?type=admin&id=".$reply_id.">".$replyName."</a>：";
					}	
				}	

			    $html='<div class="pinglun"> <img src="upload/user/small/'.$userImg.'" width="45" height="45" style="display:block; float:left;" />
			        <div class="pinglun_r">
			          <div class="pinglun_r_left"> <span><b><a href="user_main.php?type=user&id='.$user_id.'">'.$userName.'</a>：'.$reply.'</b><span class="neirong">'.$comment.'</span></span>
			            <p>'.$time.'</p>
			          </div>
			          <ul class="ul_pinglun_r">
			            <li><a href="#F1" class="a_huifu1" user_id='.$user_id.' name='.$userName.' user_type=user>回复</a></li>
			          </ul>
			        </div>
			      </div>';

			    /*回复AJAX-end*/
			}else{
				$mes='评论失败，请重试';
			}

			$error=1;
		}else{
			$error=0;
		}
		$arr=array('error'=>$error,'mes'=>$mes,'html'=>$html);
		echo json_encode($arr);
		exit();
	}
	/***评论***/



	//浏览量+1
	$rand=rand(0,1000000);
	setcookie('rand',$rand);
	//echo $_COOKIE['rand'];


	//咨询分类
	$info_class=$infoc->infoClassList();
	$smarty->assign('info_class',$info_class['result']);

	//资讯详情
	$key='i.*,FROM_UNIXTIME(i.create_time,"%Y-%m-%d") as createTime';
	$detail=$infoc->infoDetail($info_id,$key);

	//是否收藏
	$sql=$user_collection.' uc where uc.user_id="'.$user_id.'" and uc.type="info"
		  AND  uc.type_id="'.$info_id.'"';
	$is_collect=$commonc->getInfo($sql);
	$detail['is_collect']=$is_collect;


	//添加、取消收藏
	if($_GET['action']=='collect'){
		$isId=$_POST['isId'];
		if($isId>0){
			//取消收藏
			$res=$commonc->del($user_collection,$isId.',');
			$msg='取消收藏！';
		}else{
			$arr=array('user_id'=>$user_id,'type'=>'info','create_time'=>time(),'type_id'=>$_GET['id']);
			$res=$commonc->insert($user_collection,$arr);
			$id=$commonc->insert_id();
			$msg='收藏成功！';
		}
		$result=array('error'=>$res,'msg'=>$msg,'isId'=>$id);
		echo json_encode($result);
		exit();
	}
	$smarty->assign('detail',$detail);

	if($detail){
		//作者信息
		$table=$detail['type'];
		$admin_id=$detail['user_id'];
		$sql=$table.' where id="'.$admin_id.'"';
		$key='*';
		$key.=$detail['type']=='user'?',account as name':'';
		$admin=$commonc->getInfo($sql,$key);
		$smarty->assign('admin',$admin);
	}

	//添加、取消关注
	if($_GET['action']=="addFocus"){
		$isfocus=$_POST['isfocus'];			//关注、取消
		$arr=array('user_id'=>$detail['user_id'],'type'=>$detail['type'],'fans_id'=>$user_id,
					'create_time'=>time());
		if($user_id!=''){
			if($isfocus=='is'){
				$error=$commonc->insert($user_fans,$arr);
				$id=$commonc->insert_id();
				$title="取消关注";
				$msg='关注成功！';
			}else{
				$error=$commonc->del($user_fans,$user_id.',','','user_id="'.$detail['user_id'].'" and type="'.$detail['type'].'" and fans_id');
				$title="关注TA";
				$msg='关注已取消！';
			}
		}else{
			$error=2;
			$msg="您还未登录，请先登录！";
		}
		$result=array('error'=>$error,'msg'=>$msg,'title'=>$title);
		echo json_encode($result);
		exit();
	}

	//是否关注此作者
	$sql=$user_fans.' where fans_id="'.$user_id.'" and type="'.$detail['type'].'" 
		  and user_id="'.$detail['user_id'].'"';
	$isFocus=$commonc->getInfo($sql);
	$smarty->assign('isFocus',$isFocus);

	//小编说
	$where=' and a.info_id="'.$detail['id'].'"';
	$topics=$infoc->topicList($where);
	$smarty->assign('topics',$topics['result']);
	// var_dump($topics['result']);

	//用户回复(u为user_id对应;u1为reply_id对应)
	$where=' and a.info_id="'.$detail['id'].'"';
	$key="a.*,u.account,u.avatar,u1.account as account1,m.avatar as adminAvatar";
	$comment=$infoc->infoComment($where,$key);
	$comment=$comment['result'];
	if(is_array($comment)){
		foreach ($comment as $k => $v) {
			if($v['type']=='user'){
				$comment[$k]['userName']=$v['account'];
				$comment[$k]['userImg']='user/small/'.$v['avatar'];
			}elseif($v['type']=='admin'){
				$comment[$k]['userName']='小编';
				$comment[$k]['userImg']='admin/small/'.$v['adminAvatar'];
			}
			//判断是否属于回复
			if($v['reply_id']>0){
				if($v['reply_type']=='user'){
					$comment[$k]['replyName']=$v['account1'];
				}elseif($v['reply_type']=='admin'){
					$comment[$k]['replyName']='小编';
					
				}			
			}
			@$comment[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
		}	
	}
	
	$smarty->assign('comment',$comment);

	//评论数 
	$commentNum=count($comment);
	$smarty->assign('commentNum',$commentNum);

	//作者发布的文章
	$where =' and i.type="'.$detail['type'].'" and i.user_id="'.$detail['user_id'].'" 
			 and i.check="pass" ORDER BY i.create_time desc limit 0,5' ;
	$key='i.*';
	$ownInfos=$infoc->infoList($where,$key);
	$smarty->assign('ownInfos',$ownInfos['result']);


	$smarty->display("info_detail.html");
?>